///////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2009 Summit Contributors.  
// See: http://code.google.com/p/tyemill/wiki/SummitContributors
//
// The name and logo of Summit are trademarks of TYEMILL, LLC - All Rights 
// Reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a 
// copy of this software and associated documentation files (the "Software"), 
// to deal in the Software without restriction, including without 
// limitation the rights to use, copy, modify, merge, publish, distribute, 
// sublicense, and/or sell copies of the Software, and to permit persons to 
// whom the Software is furnished to do so, subject to the following 
// conditions:
//
// The above copyright notice and this permission notice shall be included 
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
///////////////////////////////////////////////////////////////////////////////

package summit.basecamp.helpers
{

import summit.basecamp.constants.BasecampOperation;
import summit.basecamp.constants.BasecampResource;
import summit.net.BasecampRequest;
import flash.net.URLRequestMethod;

/**
 * Helpers for Basecamp todo-item operations.
 */
public class TodoItemHelper
{
	/** private constants **/
	private static const TYPE:String = BasecampResource.TODOITEM;	
	private static const PATH_HELPER:String = "/todo_items/"; 
	
	/** @private **/
	public function TodoItemHelper()
	{
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for marking a todo-item as 
	 * complete.
	 * 
	 * @param id The id of the todo-item to be marked as complete.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 */ 
	public static function complete(id:String):BasecampRequest
	{ 
		SharedOperations.nullEmptyAssert(id, "id");
		
		return new BasecampRequest(
		PATH_HELPER + id + "/complete", URLRequestMethod.PUT,
		TYPE, BasecampOperation.COMPLETE);		
	}
	
	
	/**
	 * Builds a <code>BasecampRequest</code> for creating a todo-item.
	 * 
	 * <p>Typically, the XML provided is based on the template returned from
	 * a call to <code>prepare</code>.
	 * 
	 * <p><code>todo-list-id</code>, which represents the todo-list associated 
	 * with the todo-item, is required.</p>
	 * 
	 * <p>Note: The new todo-item is always placed below all other existing 
	 * todo-items (visually).</p>
	 * 
	 * @param content XML representing the todo-item to be created.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 */
	public static function create(content:XML):BasecampRequest
	{
		// POST /todo_lists/#{todo_list_id}/todo_items.xml
		// Returns HTTP status code 201 (Created) on success, with the
		
		var todoListId:String = content.child("todo-list-id")[0];
		
		SharedOperations.nullEmptyAssert(todoListId, 
		"content did not contain a valid todo-list-id", true);
		
		return new BasecampRequest("/todo_lists/" + todoListId + 
		"/todo_items.xml", URLRequestMethod.POST, TYPE, 
		BasecampOperation.CREATE,
		content);
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for deleting a todo-item.
	 * 
	 * @param id The id of the todo-item to be deleted.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
    */
	public static function destroy(id:String):BasecampRequest
	{
		return SharedOperations.destroy(id, PATH_HELPER, TYPE);
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for getting an existing todo-item
	 * from the store.
	 * 
	 * <p>The XML returned is pre-filled with the existing values of 
	 * the identified todo-item, and is ready to be resubmitted via the 
	 * <code>update</code> function.</p>
	 * 
	 * @param id The id of the todo-item to be retrieved.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 */ 
	public static function edit(id:String):BasecampRequest
	{
		return SharedOperations.edit(id, PATH_HELPER, TYPE);
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for reading a list of todo-items.
	 * Todo-items are returned in order as defined by the web UI or via the 
	 * reorder action.
	 * 
	 * @param todoListId The id of the todo-list from which the todo-items 
	 * are retrieved.
	 *
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 */ 
	public static function list(todoListId:String):BasecampRequest
	{
		SharedOperations.nullEmptyAssert(todoListId, "todoListId");
		
		// GET /todo_lists/#{todo_list_id}/todo_items.xml
		// <todo-items type="array">	
		return new BasecampRequest("/todo_lists/" + todoListId + 
		"/todo_items.xml", URLRequestMethod.GET, TYPE, 
		BasecampOperation.LIST);
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for retrieving a template for a 
	 * new todo-item.
	 * 
	 * <p>The template retrieved is used with <code>create</code> to 
	 * submit a new todo-item.</p>
	 * 
	 * @param todolistId The id of the todo-list that is to be associated with
	 * the todo-item when submitted via a call to <code>create</code>.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 */ 
	public static function prepare(todoListId:String):BasecampRequest
	{
		SharedOperations.nullEmptyAssert(todoListId, "todoListId");
		
		// GET /todo_lists/#{todo_list_id}/todo_items/new.xml
		// Just fill in the fields and submit the data as indicated by 
		// the custom X-Create-Action HTTP header	
			return SharedOperations.prepare("/todo_lists/" + 
			todoListId + "/todo_items/new.xml", "todo-list-id", todoListId, 
			TYPE); 
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for reading a single todo-item.
	 * 
	 * @param id The id of the todo-item to be read.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 */	
	 public static function read(id:String):BasecampRequest
	{
		return SharedOperations.read(id, PATH_HELPER, TYPE);
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for reordering todo-items.
	 *
	 * <p>Completed items can not be reordered.</p>
	 * 
	 * <p>Items that are not referenced in <code>content</code> are reordered
	 * below those referenced.</p>
	 * 
	 * <p>If a <code>todoListId</code> provided does not represent the 
	 * requested list of todo-items, the items are reparented. Lists can not  
	 * be reparented across project bounds.</code></p>
	 * 
	 * @param content The list of items to be reordered.
	 * @param todoListId The id of the todo-list associated with the 
	 * todo-items.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 */
	public static function reorder(content:XML, 
	todoListId:String):BasecampRequest
	{
		SharedOperations.nullEmptyAssert(todoListId, "todoListId");
		
		// POST /todo_lists/#{todo_list_id}/todo_items/reorder.xml
		// Returns HTTP status code 200 on success.
		return new BasecampRequest("/todo_lists/" + todoListId + 
		"/todo_items/reorder.xml", URLRequestMethod.POST, TYPE,
		BasecampOperation.REORDER, content); 
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for marking a todo-item
	 * as uncomplete.
	 * 
	 * @param id The id of the todo-item to be marked as uncomplete.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>.
	 */ 
	public static function uncomplete(id:String):BasecampRequest
	{
		SharedOperations.nullEmptyAssert(id, "id");
		
		// PUT /todo_items/#{id}/uncomplete.xml
		// Returns HTTP status code 200 on success.
		return new BasecampRequest(PATH_HELPER + id + "/uncomplete.xml",
		URLRequestMethod.PUT, TYPE, BasecampOperation.UNCOMPLETE); 
	}
	
	/**
	 * Builds a <code>BasecampRequest</code> for updating an existing 
	 * todo-item.
	 * 
	 * <p><code>content</code> submitted to <code>update</code> is typically
	 * retrieved from <code>edit</code>.</p>
	 * 
	 * @param content The updated XML content of an existing todo-item.
	 * 
	 * @return A request ready for use with <code>BasecampLoader.load</code>. 
	 */ 

	public static function update(content:XML):BasecampRequest
	{
		return SharedOperations.update(content, PATH_HELPER, TYPE);
	}
}
}
//////////////////////////////////////////////////////// gmarius@tyemill.com //